<?php
namespace XWAM\Addon\PXCallSystem\Controller;
/**
 * 工作日控制器
 *
 * @author Alen
 * @since 1.0 2014-7-2 by sutroon
 */
class WorkDayController extends AppbaseController {

    /**
     * 显示项目列表
     */
    public function index() {
        $this->display();
    }
    
    public function get_workday_list_json(){
        if(!$this->isSuperAdmin){
            $where['siteID']=$this->loginData['siteID'];
        }
        $list=  AddonD('WorkDay')->get_workday_list($where);
        $this->ajaxReturn($list);
    }
    public function workday_export(){
        if (!$this->isSuperAdmin) {
            die('没有权限!');
        }
        $list = AddonD('WorkDay')->scope('export')->select();
        if (!$list) {
            $this->ajaxMsg(false,'找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 显示项目编辑表单
     */
    public function item_edit($id=0) {
        if ($id) {
            $data = M('tcti_workday')->find($id);
            $this->assign('data', $data);
        }
        $this->display();
    }

    /**
     * 保存项目
     */
    public function item_save() {
        $arr=I('weekday');
        if(!is_array($arr)){
            $this->ajaxMsg(false,'每周工作日未选择!');
        }
        $params = new \Org\Su\Params();
        $params->addData('weekday', implode(',', $arr));
        $params->addData('tempHolidays');
        $params->addData('tempWorkDays');
        $id = I('id');
        $M = M('tcti_workday');
        if ($id) {
            $where['id'] = $id;
            $result = $M->where($where)->save($params->getParams());
        } else {
            $params->addData('createdTime', date('Y-m-d H:i:s'));
            $result = $M->add($params->getParams());
            $id = $result;
        }
        if ($result === false) {
            $this->ajaxMsg(false,'保存失败!');
        } else {
            $this->ajaxMsg(true,'保存成功!');
        }
    }

    /**
     * 删除项目
     */
    public function item_delete($id='') {
        if (!$id){
            $this->ajaxMsg(false,'编号丢失!');
        }            
        $result = M('tcti_workday')->delete($id);
        $this->ajaxMsg($result,'删除{%}!');
    }
}
